package edu.ncu.service.impl;

import edu.ncu.dao.CommentDao;
import edu.ncu.dao.UserDao;
import edu.ncu.entity.Comment;
import edu.ncu.entity.CommentEnum;
import edu.ncu.entity.User;
import edu.ncu.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao ;
    @Autowired
    private UserDao userDao ;

    @Override
    public List<Comment> findCommentList(CommentEnum type,int beCommentedId) {
        List<Comment> comments = null ;
        if (type == CommentEnum.PUBLISH) {
            comments = commentDao.findPublishComment(beCommentedId) ;
        } else {
            comments = commentDao.findPhotoComment(beCommentedId) ;
        }
        return comments ;
    }

    /**
     * 根据评论id查询评论，并且查询出评论所对用的所有子评论
     * @param commentId
     * @return
     */
    @Override
    public Comment findCommentById(CommentEnum type,int commentId) {
        commentDao.findChildCommentOfPhoto(commentId) ;
        Comment comment = null ;
        List<Comment> comments = null ;
        if (type == CommentEnum.PUBLISH) {
            comment = commentDao.findCommentInPublish(commentId) ;
            comments = commentDao.findChildCommentOfPublish(commentId) ;
        } else {
            comment = commentDao.findCommentInPhoto(commentId) ;
            comments = commentDao.findChildCommentOfPhoto(commentId) ;
        }
        // 查找每条子评论对应的user
        for (Comment c : comments) {
            User user = userDao.findByUserId(c.getCommenterId()) ;
            c.setUser(user);
        }
        comment.setComments(comments);
        return comment ;
    }

    @Override
    public boolean saveComment(CommentEnum type,Comment comment) {
        boolean res = false ;
        if (type == CommentEnum.PUBLISH) {
            res = commentDao.insertPublishComment(comment) ;
        } else {
            res = commentDao.insertPhotoComment(comment) ;
        }
        return res ;
    }

    /**
     * 更新子评论数量
     * @param commentId
     * @return
     */
    @Override
    public Comment updateCommentNum(CommentEnum type,int commentId) {
//        if(commentDao.updateCommentNum(commentId)) {
//            return commentDao.getCommentById(commentId) ;
//        }
        return null ;
    }

    /**
     * 更新评论点赞数量
     * @param commentId
     * @return
     */

    @Override
    public Comment updateCommentGreat(CommentEnum type,int commentId) {
        Comment res = null ;
        if (type == CommentEnum.PUBLISH) {
            if (commentDao.updatePublishCommentGreat(commentId)) {
                res = commentDao.findCommentInPublish(commentId) ;
            }
        } else {
            if (commentDao.updatePhotoCommentGreat(commentId)) {
                res = commentDao.findCommentInPhoto(commentId) ;
            }
        }
        return res ;
    }
}
